package com.sg.hms.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sg.hms.Dao.FoodDao;
import com.sg.hms.Dao.FoodMenuDao;
import com.sg.hms.Model.Food;
import com.sg.hms.Util.JDBCUtil;

public class FoodDaoImpl implements FoodDao{

	@Override
	public List<Food> findFood(int elId,int start,int pageSize) {
		List<Food> foods=new ArrayList<Food>();
		try {
			Connection con=JDBCUtil.getConnection();
			StringBuffer sql=new StringBuffer("select * from food where elId=?");
			sql.append(" limit ?,?");
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, elId);
			pst.setInt(2, start);
			pst.setInt(3, pageSize);			
			ResultSet rs=pst.executeQuery();
			while (rs.next()) {
				Food food=new Food();
				food.setElID(rs.getInt("elId"));
				food.setfName(rs.getString("fName"));
				food.setFoId(rs.getInt("foId"));
				food.setWeight(rs.getInt("weight"));
				foods.add(food);
				//System.out.println(food.getfName()+food.getFoId());
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foods;
	}

	@Override
	public List<Food> findFood(int elId, String searchName) {
		List<Food> foods=new ArrayList<Food>();
		try {
			Connection con=JDBCUtil.getConnection();
			StringBuffer sql=new StringBuffer("select * from food where elId=?");
			sql.append(" and fName like ?");
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, elId);
			pst.setString(2, "%"+searchName+"%");
			ResultSet rs=pst.executeQuery();
			//System.out.println(111);
			while (rs.next()) {
				//System.out.println(222);
				Food food=new Food();
				food.setElID(rs.getInt("elId"));
				food.setfName(rs.getString("fName"));
				food.setFoId(rs.getInt("foId"));
				food.setWeight(rs.getInt("weight"));
				foods.add(food);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foods;
	}

	@Override
	public int countFood(int elId) {
		int fcount=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select count(*) fcount from food where elId=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, elId);
			ResultSet rs=pst.executeQuery();
			//System.out.println(111);
			if (rs.next()) {
				fcount=rs.getInt("fcount");
				//System.out.println(fcount);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return fcount;
	}

	@Override
	public int addFood(int elId, String fNmae, int weight) {
		int flag=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("insert into food(elId,FName,weight) values(?,?,?)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, elId);
			pst.setString(2, fNmae);
			pst.setInt(3, weight);
			int a=pst.executeUpdate();
			//System.out.println(111);
			if (a>0) {
				flag=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

	@Override
	public int deleteFood(int elId, int foId) {
		int flag=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("delete from food where foId=? and elId=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, foId);
			pst.setInt(2, elId);
			int a=pst.executeUpdate();
			//System.out.println(111);
			if (a>0) {
				flag=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

	@Override
	public int updateFood(int foId,String fName,int weight) {
		int flag=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("update food set fName=?,weight=? where foId=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, fName);
			pst.setInt(2, weight);
			pst.setInt(3, foId);
			int a=pst.executeUpdate();
			//System.out.println(111);
			if (a>0) {
				flag=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;

 }
}